package Utils;

import java.util.Date;

import main.SharedPref;

public class DebugUtils {

	public static long startTime,elapsedTime;
	
	public static double round(double value, int places) {
		if (places < 0)
			throw new IllegalArgumentException();

		long factor = (long) Math.pow(10, places);
		value = value * factor;
		long tmp = Math.round(value);
		return (double) tmp / factor;
	}


	public static void startTimer() {
		 startTime = System.currentTimeMillis();
	}

	public static String TimeToString(long t) {

		if (!SharedPref.DEBUG)
			return null;

		StringBuilder sb = new StringBuilder();
		sb.append("execution time: ");

		if (t < 1000) {
			sb.append(t + " mili seconds");
		} else if (t > 1000 && t < 60 * 1000) {

			double dt = round((double) t / 1000, 2);
			sb.append(dt + " seconds");

		} else if (t > 60 * 1000 && t < 60 * 60 * 1000) {

			double dt = round((double) t / (60 * 1000), 2);
			sb.append(dt + " minutes");
		} else {
			double dt = round((double) t / (60 * 60 * 1000), 2);
			sb.append(dt + " hours");
		}

		return sb.toString();
 
	}


	public static long stopTimer() {
		elapsedTime = (new Date()).getTime() - startTime;
		return elapsedTime;
	}
	
	public static void initTimer(){
		startTime = 0;
		elapsedTime = 0;
	}
	
}
